package com.begye.smartloan.apptest.renzheng.views

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.View
import android.view.View.OnFocusChangeListener
import androidx.appcompat.widget.AppCompatEditText
import kotlin.math.max

class FillEditText : AppCompatEditText, TextWatcher, OnFocusChangeListener {

    constructor(context: Context) : super(context) {
        initListener()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initListener()
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initListener()
    }

    private var _isPA = false
    private var _isEA = false
    private var _isFA = false

    private var _pC = ""
    private var _fts = 0L

    private var _onFcCallback: ((String) -> Unit)? = null
    private var _onBrCallback: ((String) -> Unit)? = null
    private var _onTextChangedCallback: ((String) -> Unit)? = null

    private fun initListener() {
        this.onFocusChangeListener = this
        this.addTextChangedListener(this)
    }

    fun addFc(callback: (String) -> Unit) {
        _onFcCallback = callback
    }

    fun addBr(callback: (String) -> Unit) {
        _onBrCallback = callback
    }

    fun addTextChanged(callback: (String) -> Unit) {
        _onTextChangedCallback = callback
    }

    override fun onTextContextMenuItem(id: Int): Boolean {
        if (id == android.R.id.paste) {
            _isPA = true
        }
        return super.onTextContextMenuItem(id)
    }

    override fun onTextChanged(
        text: CharSequence?,
        start: Int,
        lengthBefore: Int,
        lengthAfter: Int
    ) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter)
        if (_isPA) {
            if (_pC.isBlank()) {
                _pC = text.toString().trim()
            }
        }
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun afterTextChanged(s: Editable?) {
        _onTextChangedCallback?.invoke(this.text.toString().trim())
    }

    override fun onFocusChange(v: View?, hasFocus: Boolean) {
        try {
            if (hasFocus) {
                _isFA = true
                _fts = System.currentTimeMillis()
                _onFcCallback?.invoke("")

            } else {
                val cruIn = this.text.toString().trim()
                _isEA = if (_isPA) {
                    if (_pC != cruIn) {
                        true
                    } else {
                        _isFA
                    }
                } else {
                    true
                }

                val btw = max(0L, (System.currentTimeMillis() - _fts)).toString()
                var type = "${cruIn}$$${btw}$$"
                if (_isPA) {
                    type = "${type}P"
                }
                if (_isEA) {
                    type = "${type}E"
                }

                _fts = 0L
                _isPA = false
                _isEA = false
                _isFA = false

                _onBrCallback?.invoke(type)
            }
        } catch (e: Exception) {
            //
        }
    }
}